//ggze前端js库
var configPro = {};
configPro.projectName = "mps";
//设置cookie
function setCookie(key, val) {
    var expiresDate= new Date();
    expiresDate.setTime(expiresDate.getTime() + (60 * 60 * 1000));
    $.cookie(key, val, {expires: expiresDate, path: '/'});
}

//获得Cookie
function getCookie(key) {
    return $.cookie(key);
}

//删除cookie
function deletedCookie() {
    document.cookie = "";
}

//ajax传参，不安全的
function playAjax(urls, json) {
    var re = "";
    $.ajax({
        url: "./dsp?code=" + urls,
        type: "POST",
        data: {"jon": json},
        async: false,
        success: function (as) {
            re = as;
        },
        error: function (es) {
            re = es;
        }
    });
    return re;
}

//安全的ajax调用
function safeAjax(urls, json) {
    var re = "";
    $.ajax({
        beforeSend: function (request) {
            request.setRequestHeader("token", getCookie("token"));
        },
        url: "./dsp?code=" + urls,
        type: "POST",
        data: {"jon": json},
        async: false,
        success: function (as) {
            re = as;
        },
        error: function (es) {
            re = es;
        }
    });
    return re;
}

//页面获得用于html内嵌，安全的获得
function playJsp(code) {
    var re = "";
    $.ajax({
        beforeSend: function (request) {
            request.setRequestHeader("token", getCookie("token"));
        },
        url: "./vsp?code=" + code,
        type: "POST",
        async: false,
        success: function (as) {
            re = as;
        },
        error: function (es) {
            re = es;
        }
    });
    return re;
}

//检测是否json
function isJSON(str) {
    if (typeof str == 'string') {
        try {
            var obj = JSON.parse(str);
            if (typeof obj == 'object' && obj) {
                return true;
            } else {
                return false;
            }
        } catch (e) {
            return false;
        }
    }
}

//公用弹出美化框
function alr(al) {
    layui.use('layer', function () {
        var layer = layui.layer;
        layer.msg(al);
    });
}

//获得表单值，以name=val组成json
function fromName(element) {
    var input = $(element).find("input");
    if(input.length==0){
        input = $(element).find("td");
    }
    var name = {};
    for (var i = 0; i < input.length; i++) {
        name[$(input[i]).attr("name")] = $(input[i]).val()!=""&&$(input[i]).val()!=null?$(input[i]).val():$(input[i]).html();
    }
    var select = $(element).find("select");
    for (var i = 0; i < select.length; i++) {
        name[$(select[i]).attr("name")] = $(select[i]).val();
    }
    return name;
}

//检测邮箱
function checkEmail(str) {
    var re = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/;
    if (re.test(str)) {
        return true;
    } else {
        return false;
    }
}

//检测手机
function checkPhone(phone) {

    if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
        return false;
    } else {

        return true;
    }
}

//检测null
function checkNull(str) {
    if (str == null || str == '') {
        return true;
    } else {
        return false;
    }
}

//检查登录
function testLogin() {
    if (getCookie("token") == 'null' || getCookie("token") == "undefined" || getCookie("token") == "" || getCookie("token") == null) {
        window.location.href = "./vsp?code=1555508458588&error=未登录请登录";
    }
}

//弹出url错误参数
function msgError() {

    var error = new acquire().parameterMap['error'];

    if (error.length > 0) {

        layui.use('layer', function () {

            var layer = layui.layer;

            layer.msg(decodeURI(error));

        });

    }

}

//弹出url参数
function acquire() {

    this.parameter;

    this.parameterMap = {};

    this.test = function () {

        if (window.location.href.split("?") <= 0) {

            return null;

        } else {

            this.parameter = window.location.href.split("?")[1];

        }

    }

    this.packaging = function () {

        for (var i = 0; i < this.parameter.split("&").length; i++) {

            var name = this.parameter.split("&")[i].split("=")[0];

            var val = this.parameter.split("&")[i].split("=")[1];

            this.parameterMap[name] = val;

        }

    }

    this.test();

    this.packaging();

}

//
function managerView(code, e) {
    $(e).html(playJsp(code));
}

function review(code, name) {
    window.parent.xadmin.add_tab(name, './vsp?code=' + code);
}

//重新记载
function reload() {
    setTimeout(function () {
        window.location.reload();
    }, 1000);
}

//单项选择
function sure(e) {
    $(".zh").removeClass("zh");
    $(e).addClass("zh");
}

//x-admin关闭当前页
function reClose() {
    setTimeout(function () {
        window.parent.$(".layui-tab-title .layui-this .layui-tab-close").click();
    }, 1000)
}

//x-admin关闭所有打开页
function reCloseAll() {
    setTimeout(function () {
        var li = window.parent.$(".layui-tab-title li");
        $(li[0]).find(".layui-tab-close").remove();
        window.parent.$(".layui-tab-title li .layui-tab-close").click();
    }, 1000)
}

//x-admin bug修复
function xb_refer() {
    setTimeout(function () {
        $.getScript('/' + configPro.projectName + '/js/xadmin.js', function () {
        });
    }, 500);
}

function referFrom() {
    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });
}

function getCode(leng) {
    var timeNUM =(new Date().getTime()).toString();
    var a=timeNUM.split(""); //先拆分成数组
    var b=timeNUM.split("").reverse(); //再反转，但还是数组
    var c=timeNUM.split("").reverse().join("");//最后把数组变成字符串
    return c.substr(0,leng);
}
function getDete(type) {
    var date = new Date();
    switch (type) {
        case "y":return date .getFullYear();break;
        case "m":return date .getMonth()+1;break;
        case "d":return date .getDate();break;
        case "r":return date .getTime();break;
        case "ym":return date .getFullYear()+"/"+(date .getMonth()+1);break;
        case "ymd":return date .toLocaleDateString();break;
        case "ymds":return date .toLocaleString( );break;
        default:return "defind";
    }
}
function editBlur(e) {
    var va=$(e).val();
    $(e).parent().html(va);
}
function loadT() {
    layui.use(['element', 'layer'], function () {
        var layer = layui.layer;
        layer.load();
    });
}
function closeLoad() {
    setTimeout(function () {
        layui.use('element', function () {
            var element = layui.element;
            layer.closeAll('loading');
        });
    },200);
}